var express = require('express');
var router = express.Router();
const { userLogin } = require('../controller/userController');
const { SuccessModel, ErrorModel } = require('../model/resModel')

/* GET home page. */
router.post('/login', function (req, res, next) {
  const { username, password } = req.body;
  // const { username, password } = req.query
  const userDate = userLogin(username, password);
  return userDate.then(data => {
    console.log(data)
    if (data.username) {
      // 设置 session
      req.session.username = data.username
      req.session.realname = data.realname

      res.json(new SuccessModel(data))
    } else {
      res.json(new ErrorModel('登录失败'))
    }
  })

});

module.exports = router;
